#if !USE_CONSOLE

#include "src/interface/scenePanel.h"

#include "src/scene/ambientLight.h"
#include "src/scene/areaLight.h"

#include <iostream>

ScenePanel::ScenePanel(GLWidget *_glWidget, QWidget* _parent, Scene *_scene) : QWidget(_parent), scene(_scene){
    objectsTree = new TreeWidget("Objetos", _glWidget, this);
    lightsTree = new TreeWidget("Luzes", _glWidget, this);

    addFirstLights();

    connect(objectsTree, SIGNAL(deleteObjectSignal(Object *)), this, SLOT(deleteObject(Object *)));
    connect(objectsTree, SIGNAL(addObjectSignal(Object *)), this, SLOT(addObject(Object *)));

    Color background = _scene->getBackgroundColor();
    backgroundR = background.red * 255.0;
    backgroundG = background.green * 255.0;
    backgroundB = background.blue * 255.0;

    backgroundPushButton = new QPushButton(tr("Cor de fundo"));
    connect(backgroundPushButton, SIGNAL(clicked()), this, SLOT(changeBackground()));
    setButtonColor();

    //------------------------------------------------------------
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(backgroundPushButton);
    layout->addWidget(objectsTree);
    layout->addWidget(lightsTree);
	layout->addStretch(1);

    setLayout(layout);
}

ScenePanel::~ScenePanel()
{
    delete backgroundPushButton;
    delete objectsTree;
    delete lightsTree;
}

void ScenePanel::setButtonColor(){
    QString backgroundStr;
    if(backgroundR + backgroundB + backgroundG < 400){
        backgroundStr = QString::fromUtf8("color: white;\n");
        backgroundStr.append(QString::fromUtf8("border: 1px solid white;"));
    }else{
        backgroundStr = QString::fromUtf8("color: black;\n");
        backgroundStr.append(QString::fromUtf8("border: 1px solid black;"));
    }
    backgroundStr.append(QString::fromUtf8("background-color: rgb("));
    backgroundStr.append(QString::number(backgroundR));
    backgroundStr.append(QString::fromUtf8(", "));
    backgroundStr.append(QString::number(backgroundG));
    backgroundStr.append(QString::fromUtf8(", "));
    backgroundStr.append(QString::number(backgroundB));
//    backgroundStr.append(QString::fromUtf8(", "));
//    backgroundStr.append(QString::number(backgroundA));
    backgroundStr.append(QString::fromUtf8(");"));
    backgroundStr.append(QString::fromUtf8("height: 20px;"));
    backgroundStr.append(QString::fromUtf8("border-radius: 7px;"));

    backgroundPushButton->setStyleSheet(backgroundStr);

}

void ScenePanel::changeBackground(){
    QColor color = QColorDialog::getColor(QColor(backgroundR, backgroundG, backgroundB/*, backgroundA*/), this, "Escolha a cor fundo", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        backgroundR = color.red();
        backgroundG = color.green();
        backgroundB = color.blue();
//        backgroundA = color.alpha();
    }
    setButtonColor();
    scene->setBackgroundColor(Color(backgroundR/255.0, backgroundG/255.0, backgroundB/255.0));
}

void ScenePanel::setScene(Scene *_s)
{
    //criando uma nova cena vazia
    scene = _s;
    scene->setBackgroundColor(Color(backgroundR/255.0, backgroundG/255.0, backgroundB/255.0));
    objectsTree->clear();
    lightsTree->clear();
    addFirstLights();
}

void ScenePanel::update(Scene *_s, std::vector<Object *> *_objs)
{

    //updatando ou criando uma nova cena de arquivo
    if(_s){
        scene = _s;
        if(scene->getLightsSize() < 1)
        {
            addFirstLights();
        }

        Color background = scene->getBackgroundColor();
        backgroundR = background.red * 255.0;
        backgroundG = background.green * 255.0;
        backgroundB = background.blue * 255.0;
        setButtonColor();
        lightsTree->updateLights(scene);
    }

    //só vai reorganizar os ids, e so tiver entrnaod objetos, isso acontece quando não se tem ma nova cena
    objectsTree->updateObjects(_objs, scene);//de qalquer modo vai entrar nessa função, sej apra adicionar, ou para trocar
}

void ScenePanel::addFirstLights()
{
    Light *light1 = new AmbientLight(scene->nextLightsID(), Color(0.5, 0.5, 0.5));
    Light *light2 = new AreaLight(scene->nextLightsID(), Vector3D(-0.5, -0.5, 4.0), Vector3D(1, 0, 0), Vector3D(0, 1, 0), Color(0.5, 0.5, 0.5));
    lightsTree->addLight(light1);
    lightsTree->addLight(light2);
    scene->addLight(light1);
    scene->addLight(light2);
}
#endif //!USE_CONSOLE
